<!-- token管理 -->
<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :model="form" :inline="true">
        <el-form-item>
          <el-select v-model="form.chain_name" placeholder="主链名称">
            <el-option
              v-for="item in chainsList"
              :key="item.chain_id"
              :label="item.chain_name"
              :value="item.chain_name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.coin_id" placeholder="币种名称">
            <el-option
              v-for="item in coinsList"
              :key="item.coin_id"
              :label="item.coin_name"
              :value="item.coin_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.token_name" placeholder="Token名称">
            <el-option
              v-for="item in tokensList"
              :key="item.token_id"
              :label="item.token_name"
              :value="item.token_name"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="TokenID" align="center" />
          <el-table-column prop="coin_id" label="币种ID" align="center" />
          <el-table-column
            prop="coin_name"
            label="币种名称"
            align="center"
            width="110"
          />
          <el-table-column
            prop="token_name"
            label="Token名称"
            align="center"
            width="110"
          />
          <el-table-column
            prop="chain_name"
            label="主链名称"
            align="center"
            width="110"
          />
          <el-table-column prop="chain_alias" label="主链别名" align="center" />
          <el-table-column
            prop="cn_name"
            label="Token中文名称"
            align="center"
          />
          <el-table-column
            prop="en_name"
            label="Token英文名称"
            align="center"
          />
          <el-table-column
            prop="block_base_url"
            label="交易查询地址"
            align="center"
            width="200"
          />
          <el-table-column
            prop="is_active"
            label="是否启用"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="is_memo"
            label="是否Memo类"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="is_deposit"
            label="是否允许充值"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="is_withdraw"
            label="是否允许提现"
            align="center"
            :formatter="formatter"
          />
          <el-table-column prop="min_fee" label="提现费用" align="center" />
          <el-table-column
            prop="min_deposit"
            label="最小充值数量"
            align="center"
          />
          <el-table-column
            prop="min_withdraw"
            label="最小提现数量"
            align="center"
          />
          <el-table-column
            prop="max_withdraw"
            label="最大提现数量"
            align="center"
          />
          <el-table-column prop="confirmations" label="确认数" align="center" />
          <el-table-column
            prop="contract"
            label="合约地址"
            align="center"
            width="200"
          />
          <el-table-column
            prop="contract_precision"
            label="合约精度"
            align="center"
          />
          <el-table-column
            prop="deposit_address"
            label="入账地址"
            align="center"
          />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            prop="updatetime"
            label="更新时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            label="操作"
            align="center"
            width="100"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="onEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogFormVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatterTime, formatter } from "@/utils/utils";
import { tokensListApi, tokensEditApi } from "@/api/ExAssistant/wallet";
import { GlobalStore } from "@/store";
import DialogForm from "./components/dialogForm.vue";
const globalStore = GlobalStore();

const coinsList = computed(() => globalStore.coins);
const chainsList = computed(() => globalStore.chains);
const tokensList = computed(() => globalStore.tokens);
const tableData = ref([]);
const loading = ref(false);
const dialogFormVisible = ref(false);
const btnLoading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const formData = {
  coin_id: "",
  chain_name: "",
  token_name: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  token_id: "",
  coin_id: "",
  token_name: "",
  cn_name: "",
  en_name: "",
  chain_name: "",
  chain_alias: "",
  chain_url: "",
  is_active: false,
  is_memo: false,
  is_deposit: false,
  is_withdraw: false,
  min_fee: "",
  min_deposit: "",
  min_withdraw: "",
  max_withdraw: "",
  confirmations: "",
  contract: "",
  contract_precision: "",
  deposit_address: "",
};

const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    chain_name: form.chain_name,
    coin_id: form.coin_id,
    token_name: form.token_name,
  };
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = () => {
  getTbaleData();
};
// 新增
const onAdd = () => {
  dialogFormVisible.value = true;
};
// 编辑
const onEdit = (index, row) => {
  Object.assign(dialogForm, {
    ...row,
    is_deposit: row.is_deposit == 1 ? true : false,
    is_withdraw: row.is_withdraw == 1 ? true : false,
    is_active: row.is_active == 1 ? true : false,
    is_memo: row.is_memo == 1 ? true : false,
    chain_url: row.block_base_url,
    token_id: row.id,
  });
  dialogFormVisible.value = true;
};
//关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  console.log(param);
  try {
    btnLoading.value = true;
    const res = await tokensEditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogFormVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogFormVisible.value = false;
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await tokensListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
<style>
.el-form--inline .el-form-item {
  margin-right: 20px;
}
</style>
